import random


side_deck_possibilities = [-1, -2, -3, -4, -5, -6, 1, 2, 3, 4, 5, 6]
table_deck = []

class Player:
      def __init__(self, name):
            self.side_deck = [side_deck_possibilities[random.randint(0, len(side_deck_possibilities)-1)] for card in range(0, 4)]
            self.running_total = 0
            self.is_standing = False
            self.is_busted = False
            self.name = name
            self.wins = 0

      def play_side_card(self):
            print("You have chosen to play a side card, here is what you have left:\n")
            for i in range(0, len(self.side_deck)):
                  print("[{c}] {card}".format(c = i, card = self.side_deck[i]))
            choice = input("Which card would you like to play?\n")
            self.running_total += self.side_deck[int(choice)]
            del self.side_deck[int(choice)]

      def hit(self):
            index = random.randint(0, len(table_deck) - 1)
            drawn = table_deck[index]
            print("{} drew a {}\n".format(self.name, drawn))
            self.running_total += table_deck[index]
            del table_deck[index]
            if self.running_total > 20:
                  print("{} has busted!\n".format(self.name))
                  self.is_busted = True

      def prompt(self):
            choosing = True
            print("What would you like to do {n}\n".format(n = self.name))
            print("Your current value is: {}\n".format(self.running_total))
            print("Your current side deck is:{}\n".format(self.side_deck))
            while(choosing == True):
                  print("Options: [1] Hit | [2] Stand | [3] Play from side deck\n")
                  choice = input()
                  if choice == '1':
                        choosing = False
                        self.hit()

                  elif choice == '2':
                        choosing = False
                        self.is_standing = True

                  elif choice == '3':
                        choosing = False
                        self.play_side_card()
                  else:
                        print("Not a valid choice, what would you like to do?\n")

def reset_deck(player1, player2):
      for i in range(10):
            for j in range(4):
                  table_deck.append(i+1)
      player1.is_busted = False
      player1.is_standing = False
      player2.is_busted = False
      player2.is_standing = False
      player1.running_total = 0
      player2.running_total = 0

def run_game():
      is_running = True
      player1 = Player("Dave")
      player2 = Player("Jett")
      reset_deck(player1, player2)

      while(is_running):
            # check the number of wins each player has, and if they hit three then they are the winner
            if player2.wins == 3:
                  print("{} has one the game! Congratulations!\n".format(player2.name))
                  is_running = False

            elif player1.wins == 3:
                  print("{} has one the game! Congratulations!\n".format(player1.name))
                  is_running = False

            # check to see whether either player has busted
            if player1.is_busted:
                  print("{} wins this round\n".format(player2.name))
                  player2.wins += 1
                  reset_deck(player1, player2)

            elif player2.is_busted:
                  print("{} wins this round\n".format(player1.name))
                  player1.wins += 1
                  reset_deck(player1, player2)

            # check to see whether a player is standing, and if they are skip their turn
            if not player1.is_standing and not player1.is_busted:
                  player1.prompt()

            if not player2.is_standing and not player2.is_busted:
                  player2.prompt()

            # if/when both players are standing, determine a winner based on who has the closest stack to 20
            if player2.is_standing and player1.is_standing:

                  if player1.running_total > player2.running_total:
                        print("{} has the higher score and wins the round!".format(player1.name))
                        player1.wins += 1
                        reset_deck(player1, player2)

                  elif player2.running_total > player1.running_total:
                        print("{} has the higher score and wins the round!".format(player2.name))
                        player2.wins += 1
                        reset_deck(player1, player2)

                  else:
                        print("Both players have tied, no points awarded")
                        reset_deck(player1, player2)

run_game()
